from django.shortcuts import render, HttpResponse, redirect, reverse
from django.views.decorators.http import require_POST, require_GET
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required

from xadmin.models import Part, PartElement
from xadmin.forms import PartForm
from xadmin.plugins.auditlog import add_log


@login_required
def index(request):
    mbx = ('部件管理',)
    siteid = request.GET.get('siteid')
    parts = Part.objects.filter(site_id=siteid)
    context = {
        'siteid': siteid,
        'mbx': mbx,
        'parts': parts
    }
    return render(request, 'xadmin/part/list.html', context)


@login_required
def add(request):
    siteid = request.GET.get('siteid', '1')
    mbx = ('部件管理', '添加部件')
    context = {
        'siteid': siteid,
        'mbx': mbx,
    }
    if request.method == 'GET':
        part_from = PartForm.PartForm()
        context['part_form'] = part_from
        return render(request, 'xadmin/part/add.html', context)
    else:
        part_form = PartForm.PartForm(request.POST)
        if part_form.is_valid():
            new_part = part_form.save(commit=False)
            new_part.site_id = siteid
            new_part.save()
            action = '添加了部件《%s》' % new_part.name
            add_log(request, action, siteid)
            return redirect(reverse('partlist') + '?siteid=' + siteid)
        else:
            error = part_form.errors
            context['error'] = error
            context['part_form'] = part_form
            return render(request, 'xadmin/part/add.html',context)


@login_required
def edit(request):
    siteid = request.GET.get('siteid', '1')
    mbx = ('部件管理', '编辑部件')
    partid = request.GET.get('partid')
    context = {
        'siteid': siteid,
        'mbx': mbx,
    }
    part_obj = Part.objects.get(id=partid)
    if request.method == 'GET':
        part_form = PartForm.PartForm(instance=part_obj)
        context['part_form'] = part_form
        return render(request, 'xadmin/part/add.html',context)
    else:
        part_form = PartForm.PartForm(request.POST, instance=part_obj)
        if part_form.is_valid():
            part_form.save()
            action = '修改了部件《%s》' % part_form.cleaned_data['name']
            add_log(request, action, siteid)
            return redirect(reverse('partlist') + '?siteid=' + siteid)
        else:
            error = part_form.errors
            context['error'] = error
            context['part_form'] = part_form
            return render(request, 'xadmin/part/add.html', context)


@login_required
@require_POST
def part_del(request):
    partid = request.POST.get('partid')
    try:
        part = Part.objects.get(id=partid)
        part.delete()
        message = {
            'status': '200',
            'message': '删除成功'
        }
        action = '修改了部件《%s》' % part.name
        add_log(request, action, part.site_id)
    except:
        message = {
            'status': 200,
            'message': '删除失败'
        }
    return JsonResponse(message)